package com.whately.core.database.common;

import com.whately.core.database.common.srv.CoreSequenceFactory;
import com.whately.core.database.common.srv.CoreSequenceTxFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CoreSequenceUtil {

    private static CoreSequenceFactory sequenceFactory;

    private static CoreSequenceTxFactory sequenceTxFactory;

    @Autowired
    public CoreSequenceUtil(CoreSequenceFactory coreSequenceFactory,CoreSequenceTxFactory sequenceTxFactory) {
        CoreSequenceUtil.sequenceFactory = coreSequenceFactory;
        CoreSequenceUtil.sequenceTxFactory = sequenceTxFactory;
    }

    /**
     * 直接生成，不起事务
     */
    public static Long generate(String sequenceCode){
        return sequenceFactory.updateAndGetIncreasedSequenceByCodeNotSupportTx(sequenceCode).getSequenceValue();
    }

    /**
     * 起事务 ，可以回滚，慢
     * 对于特殊情况使用：比如需要连续的序列
     */
    public static Long openTransactionGenerate(String sequenceCode){
        return sequenceTxFactory.updateAndGetIncreasedSequenceByCodeOpenTx(sequenceCode).getSequenceValue();
    }


    /**
     * 默认,取序列的时候，不需要事务
     * （事务的传播级别设置为Not_SUPPORT , 如果存在事务，则挂起当前事务后运行，不存在直接运行）
     */
    /*public  static CoreSequenceFactory coreSequenceService(){
        return sequenceFactory;
    }*/
}
